Imports PTT.CarRes.Data

Public Class ClassQueryInvCarBrand
    Public Function Add(ByVal InvCarBrand As QueryInvCarBrand, ByVal Strconn As String) As Boolean
        Dim ClsInvCarBrand As New ClassInvCarBrand
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarBrand.ConnectionOpen(Strconn)
        Dim ret As Boolean = False
        With ClsInvCarBrand
            .BrandId = InvCarBrand.BrandId
            .BrandName = InvCarBrand.BrandName
            .BrandStatus = InvCarBrand.BrandStatus
            ret = .USP_Save(Conn)
        End With
        ClsInvCarBrand.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvCarBrand As QueryInvCarBrand, ByVal Strconn As String) As Boolean
        Dim ClsInvCarBrand As New ClassInvCarBrand
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarBrand.ConnectionOpen(Strconn)
        Dim ret As Boolean = False
        With ClsInvCarBrand
            .BrandId = InvCarBrand.BrandId
            .Load(Conn)
            .BrandName = InvCarBrand.BrandName
            .BrandStatus = InvCarBrand.BrandStatus
            ret = .USP_Update(Conn)
        End With
        ClsInvCarBrand.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvCarBrand As QueryInvCarBrand, ByVal Strconn As String) As Boolean
        Dim ClsInvCarBrand As New ClassInvCarBrand
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarBrand.ConnectionOpen(Strconn)
        Dim ret As Boolean = False
        With ClsInvCarBrand
            .BrandId = InvCarBrand.BrandId
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvCarBrand.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaBrandId As String = "", _
    Optional ByVal ParaBrandName As String = "", _
    Optional ByVal ParaBrandStatus As String = "", Optional ByVal Strconn As String = "") As QueryInvCarBrands
        Dim ClsInvCarBrand As New ClassInvCarBrand
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarBrand.ConnectionOpen(Strconn)
        Dim ret As QueryInvCarBrands = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarBrand"
        If ParaBrandId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(BrandId = " & ParaBrandId & ")"
        End If
        If ParaBrandName & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(BrandName like '%" & ParaBrandName & "%')"
        End If
        If ParaBrandStatus & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(BrandStatus like '%" & ParaBrandStatus & "%')"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvCarBrand
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarBrands

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvCarBrand( _
                              .Item("BrandId") & "", _
                              .Item("BrandName") & "", _
                              .Item("BrandStatus") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarBrand.ConnectionClose(Conn)
        Return ret
    End Function
    Public Function Load(Optional ByVal ParaBrandId As String = "", Optional ByVal Strconn As String = "") As QueryInvCarBrands
        Dim ClsInvCarBrand As New ClassInvCarBrand
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarBrand.ConnectionOpen(Strconn)
        Dim ret As QueryInvCarBrands = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarBrand"
        If ParaBrandId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(BrandId = " & ParaBrandId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvCarBrand
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarBrands

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvCarBrand( _
                              .Item("BrandId") & "", _
                              .Item("BrandName") & "", _
                              .Item("BrandStatus") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarBrand.ConnectionClose(Conn)
        Return ret
    End Function
End Class